<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="business.IndexItemSimulitude"%>
<%@page import="business.Document"%>
<%@page import="business.IndexItem"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<jsp:useBean id="userBean" class="struts.actions.UserAction" scope="session"/>
<jsp:setProperty name="userBean" property="*"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <title>SMRI Searcher</title>
        <link href="css/home.css" rel="stylesheet" type="text/css">
        <link href="../css/home.css" rel="stylesheet" type="text/css">

        <style type="text/css">
            <!--
            body { background-color: #76AFCA; }
            #AutoNumber1 { text-align: center; width: 50%; height: 50%; background-color: #76AFCA; border: 0px solid; }
            #AutoNumber1 { margin-left: auto; margin-right: auto; }
            #left_td { width: 0px; font-size: 0px; line-height: 0px; margin: 0px; padding: 0px; }
            #container_td { vertical-align: top; }
            #top_td { height: 10%; }
            #bottom_td { height: 90%; }
            .q, a.q:active, a.q:link, a.q:hover, a.q:visited  { color :#292421; }
            -->
        </style>
        <script>
        var current_menu = false;
        function show_menu(menu_name, link) {
            var new_menu = document.getElementById("menu_" + menu_name);
            //if (new_menu != current_menu) {
		if (current_menu) {
			current_menu.style.display = "none";
		}
		new_menu.style.display = "block";
		current_menu = new_menu;
            //}
        }
        function hide_menu(menu_name, link) {
            var new_menu = document.getElementById("menu_" + menu_name);
            if (new_menu == current_menu) {
		current_menu.style.display = "none";
		if (current_submenu) {
			current_submenu.style.display = "none";
		}
		current_menu = false;
		current_submenu = false;
            }
        }

        function noReturn(s)
        {
            new_s = "";
            if (s!=null)
            {
                i = s.length;
                j = 0;
                while(j<i)
                {
                    if(s.charCodeAt(j)!=13 && s.charCodeAt(j)!=10)
                        new_s = new_s + s.charAt(j);
                    else
                        new_s = new_s + " ";
                    j++;
                }
            }
            return new_s;
        }

        function show(idLayer)
        {
            //alert(document.getElementById("layer_" + idLayer).style.visibility);
            if(document.getElementById("layer_" + idLayer).style.visibility == "visible")
            {
                document.getElementById("layer_" + idLayer).style.height = "0px";
                document.getElementById("layer_" + idLayer).style.visibility = "hidden";                
            }
            else
            {
                document.getElementById("layer_" + idLayer).style.height = "auto";
                document.getElementById("layer_" + idLayer).style.visibility = "visible";                
            }

            var searchText = "<%= userBean.getCleanQuery() %>";
            var colors = ["#F00", "#FF0", "#F0F", "#0F0", "#0FF", "#00F"];

            var stSearch = searchText.split(" ");            
            var text = noReturn(document.getElementById("layer_" + idLayer).innerHTML);
            var st = text.split(" ");
            var sText = "";
            var flag = false;

            for(j=0; j<st.length; j++)
            {                
                flag = false;                
                for(i=0; i<stSearch.length; i++)
                {
                    if(st[j].indexOf(stSearch[i]) != -1) {
                        sText += "<font style=\"color: "+colors[i]+"; font-weight: bold\"> "+ st[j] +" </font>";
                        flag = true;
                    }
                }
                if(!flag)
                        sText += st[j] + " ";
            }
            document.getElementById("layer_" + idLayer).innerHTML = sText;
        }
     </script>
    </head>

    
    <body style="overflow: scroll">

        <div id="overlay" align="center" valign="middle">&#160;</div>
        <div id="getmenu_container">
            <div id="menubar">
                <ul class="menubar">
                    <li onmouseover="show_menu('check_email');" onmouseout="hide_menu('check_email');">
                            <div><a href="#" class="root" onclick="return false;">Desarrollado por▼</a></div>
                            <div class="popup_menu" id="menu_check_email">
                                <div><a href="">Eduardo Moreno</a></div>
                                <div><a href="">Jos&eacute; Mar&iacute;a Romero</a></div>
                                <div><a href="">Manuel Jes&uacute;s Iglesias</a></div>
                            </div>
                    </li>
                </ul>
            </div>
        </div>

        <table border="0" cellpadding="0" cellspacing="0" width="80%">
            <tr style="height: 50px"></tr>
            <tr style="height: 50px">
                <td style="width: 100px"></td>
                <td valign="middle">
                    <img style="width: 200px" alt="" src="http://74.205.46.130/f/logogen.php?lstyle=bold-cartoon&amp;ltext=SMRI Searcher&amp;lsize=64&amp;lcolor=E6E066&amp;lbcolor=76AFCA&amp;ladj=1&amp;btran=0&amp;lmargin=20" id="logo_img">
                </td>                
                <td>
                    <form action="index.jsp" method="post">
                        <table cellSpacing="0" cellPadding="0" style="margin-left: auto; margin-right: auto">
                            <tr vAlign="top">
                                <td width="25%" style="font-family: arial,sans-serif">&nbsp;</td>
                                <td noWrap valign="middle" align="center" style="font-family: arial,sans-serif">
                                    <input type="text" name="searchText" id="searchText" size="80" autocomplete="on" value="<%= userBean.getSearchText() %>"/>
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <input type="submit" name="sa" value="Search" />
                                    <script type="text/javascript">
                                                write_buttons("");
                                                load_menu();
                                             </script>
                                </td>
                                <td noWrap width="25%" style="font-family: arial,sans-serif"><font size="-2">&nbsp;</font></td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
            <tr style="height: 35px"></tr>
            <%
                if(userBean.getSearchResult().size() > 0)
                {
                    out.print("<tr><td></td><td colspan=\"10\">");
                    out.print("<font style=\"font-weight: bold;\"> "+ userBean.getSearchResult().size()+" resultados en "+userBean.getSearchTime()+" mseg</font>");
                    out.print("</td></tr><tr style=\"height: 20px\"></tr>");

                    int npage = 0;
                    if(request.getParameter("page") != null)
                        npage = Integer.parseInt(request.getParameter("page"));

                    List list = userBean.getSearchResultPaginate(npage);
                    for(int i=0; i<list.size(); i++)
                    {
                        %>
                    <tr>
                        <td style=""></td>
                        <td style="height: 50px;" colspan="10" valign="top">
                            <%
                                        IndexItemSimulitude is = (IndexItemSimulitude)list.get(i);
                                        Document doc = userBean.getDocumentById(is.getIndexItem().getDocReference());
                                        NumberFormat nf = NumberFormat.getInstance();
                                        nf.setMaximumFractionDigits(2);
                                       %>
                             <form id="form_<%= i %>" method="post" action="resultsearch.jsp?page=<%= npage %>&idfile=<%= i %>">
                                <a style="font-size: 17px; color: blue;" href="javascript: document.getElementById('form_<%= i %>').submit()">
                                 <%= npage*userBean.NPAGES+i+1 %>)
                                 <%= userBean.capitalizeSentence(doc.getTittle()) %> (<%= nf.format(is.getSimilitude()) %>) (<%= doc.getId() %>)
                                 </a>
                                 <br/>
                                 <font style="font-size: 12px; color: darkgreen;"><%= userBean.getFileName(doc.getFileId()) %></font><br/>
                                 <%
                                            if(request.getParameter("idfile") != null && Integer.parseInt(request.getParameter("idfile")) == i)
                                            {
                                                %>
                                      <div id="layer_<%= i %>" style="visibility: hidden;" >
                                          <%= userBean.getDocumentText(doc) %><br/><br/>
                                      </div>
                                      <script>show(<%= i %>); document.getElementById("layer_<%= i %>").focus();</script>
                                    <%
                                            }
                                            %>
                                 
                             </form>
                        </td>
                    </tr>                                        
                  <%
                    }
                }
                else
                {
                    %>
                   <tr>
                       <td></td>
                       <td colspan="10"><font style="font-weight: bold">No se han encontrado resultados</font> </td>
                   </tr>
               <%
                }
                %>
            <tr>
                <td></td>
            <%

                int npage = 0;
                if(request.getParameter("page") != null)
                    npage = Integer.parseInt(request.getParameter("page"));
                if(npage > 0)
                {
                     %>
                    <td><a href="resultsearch.jsp?page=<%= npage-1 %>">Anterior</a> </td>
                <%
                }


                if((npage+1) * userBean.NPAGES < userBean.getSearchResult().size())
                {
                     %>
                    <td><a href="resultsearch.jsp?page=<%= npage+1 %>">Siguiente</a> </td>                
                <%
                }
                %>
            </tr>
            <tr><td colspan="10" style="height: 20px"></td></tr>
        </table>                
    </body>
</html>
